/**	
 * Author : Mht
 * Desc : For 2016 RDM 
 */
package schedule.listener;

import org.apache.commons.lang3.StringUtils;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobListener;

import entity.schedule.ScheduleJob;
import lombok.extern.log4j.Log4j;

/**
 * @author Mht
 * @date Jun 2, 2016
 */
@Log4j
public class RedisJobListener implements JobListener {

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.quartz.JobListener#getName()
	 */
	private String name;

	public RedisJobListener() {
		super();
	}

	public RedisJobListener(String name) {
		this.name = name;
	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return name;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.quartz.JobListener#jobExecutionVetoed(org.quartz.JobExecutionContext)
	 */
	@Override
	public void jobExecutionVetoed(JobExecutionContext context) {
		// TODO Auto-generated method stub
		JobDataMap jobDataMap = context.getMergedJobDataMap();
		ScheduleJob scheduleJob = (ScheduleJob) jobDataMap.get("scheduleJob");
		log.info(scheduleJob.getJobName() + "准备要开始执行啦……");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.quartz.JobListener#jobToBeExecuted(org.quartz.JobExecutionContext)
	 */
	@Override
	public void jobToBeExecuted(JobExecutionContext context) {
		// TODO Auto-generated method stub
		JobDataMap jobDataMap = context.getMergedJobDataMap();
		ScheduleJob scheduleJob = (ScheduleJob) jobDataMap.get("scheduleJob");
		log.info(scheduleJob.getJobName() + "要开始执行啦……");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.quartz.JobListener#jobWasExecuted(org.quartz.JobExecutionContext,
	 * org.quartz.JobExecutionException)
	 */
	@Override
	public void jobWasExecuted(JobExecutionContext context, JobExecutionException exception) {
		// TODO Auto-generated method stub
		JobDataMap jobDataMap = context.getMergedJobDataMap();
		ScheduleJob scheduleJob = (ScheduleJob) jobDataMap.get("scheduleJob");
		String jobName = scheduleJob.getJobName();
		if (null != exception) {
			String errorMsg = exception.toString();
			if (StringUtils.isNotEmpty(errorMsg)) {
				log.error("JobName:[" + jobName + "] throws " + errorMsg + "!");
			}
		} else {
			log.info("JobName:[" + jobName + "] was executed " + context.getJobRunTime() + "s sucessfully.");
		}
	}

}
